package com.liwang.lanouhn.nvshengo.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.liwang.lanouhn.nvshengo.Info.FeatureInfo;
import com.liwang.lanouhn.nvshengo.Info.Info;
import com.liwang.lanouhn.nvshengo.Info.NewFeatureInfo;
import com.liwang.lanouhn.nvshengo.Info.TutorialInfo;
import com.liwang.lanouhn.nvshengo.R;
import com.liwang.lanouhn.nvshengo.activities.FeatureDetailsActivity;
import com.liwang.lanouhn.nvshengo.activities.TutorialDetailsActivity;
import com.liwang.lanouhn.nvshengo.view.RecyclerImageView;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by lanouhn on 2016/10/8.
 */
public class FragmentCosmeticAdapter extends RecyclerView.Adapter {

    private FeatureRecyclerViewAdapter featureRecyclerViewAdapter;
    private Context context;
    private List<Map<Integer, Object>> totalMap;



    public FragmentCosmeticAdapter(Context context, List<Map<Integer, Object>> maps) {
        this.context = context;
        this.totalMap = maps;

    }

    @Override
    public int getItemViewType(int position) {

        return super.getItemViewType(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View view = LayoutInflater.from(context).inflate(R.layout.item_cosmetic, null);
        holder = new CosmeticViewHolder(view);
        return holder;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {


        final CosmeticViewHolder cosmeticViewHolder = (CosmeticViewHolder) holder;
        Integer type = 0;//判断类型的值

        Map<Integer, Object> map = totalMap.get(position);
        Iterator i = map.entrySet().iterator();
        while (i.hasNext()) {//只遍历一次,速度快
            Map.Entry e = (Map.Entry) i.next();
            type = (Integer) e.getKey();
        }

        switch (type) {
            case 0:
                final TutorialInfo tutorialInfo = (TutorialInfo) map.get(type);
                cosmeticViewHolder.ll_Tutorial.setVisibility(View.VISIBLE);
                cosmeticViewHolder.ll_background.setVisibility(View.GONE);
                cosmeticViewHolder.rl_Feature.setVisibility(View.GONE);
                Picasso.with(context).load(tutorialInfo.getAvatar_url()).fit().config(Bitmap.Config.RGB_565).into(cosmeticViewHolder.iv_head);
                cosmeticViewHolder.tv_name.setText(tutorialInfo.getUsername());
                Picasso.with(context).load(tutorialInfo.getUrl()).fit().config(Bitmap.Config.RGB_565).into(cosmeticViewHolder.iv_icon);
                String url =tutorialInfo.getVideo_url();
                long date_published = tutorialInfo.getTimestamp();
                long date = date_published * 1000L;
                Date d = new Date(date);
                SimpleDateFormat format = new SimpleDateFormat("mm-ss");
                String dateStr = format.format(d);
                cosmeticViewHolder.btn_time.setText("7'15");
                cosmeticViewHolder.tv_title.setText(tutorialInfo.getTitle());
                cosmeticViewHolder.tv_look.setText(tutorialInfo.getView_count() + "");
                cosmeticViewHolder.tv_comment.setText(tutorialInfo.getComment_count() + "");
                cosmeticViewHolder.tv_like.setText(tutorialInfo.getLike_count() + "");
                cosmeticViewHolder.btn_attention.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        cosmeticViewHolder.btn_attention.setBackgroundResource(R.mipmap.ic_followed);

                    }
                });
                cosmeticViewHolder.ll_details.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent =new Intent(context,TutorialDetailsActivity.class);
                        intent.putExtra("info",tutorialInfo);
                        context.startActivity(intent);
                    }
                });


                break;
            case 1:
                final NewFeatureInfo newFeatureInfo = (NewFeatureInfo) map.get(type);
                cosmeticViewHolder.ll_Tutorial.setVisibility(View.GONE);
                cosmeticViewHolder.ll_background.setVisibility(View.VISIBLE);
                cosmeticViewHolder.rl_Feature.setVisibility(View.GONE);
                Picasso.with(context).load(newFeatureInfo.getUrl()).fit().config(Bitmap.Config.RGB_565).into(cosmeticViewHolder.iv_background);
                cosmeticViewHolder.iv_background.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Info info =new Info();
                        info.setTitle(newFeatureInfo.getTitle());
                        info.setLike_count(newFeatureInfo.getLike_count());
                        info.setUrl(newFeatureInfo.getNexturl());
                        Intent intent =new Intent(context,FeatureDetailsActivity.class);
                        intent.putExtra("featureInfo",info);
                        context.startActivity(intent);
                    }
                });
                break;
            case 2:
                final FeatureInfo featureInfo = (FeatureInfo) map.get(type);
                cosmeticViewHolder.ll_Tutorial.setVisibility(View.GONE);
                cosmeticViewHolder.ll_background.setVisibility(View.GONE);
                cosmeticViewHolder.rl_Feature.setVisibility(View.VISIBLE);
                cosmeticViewHolder.tv_caption.setText(featureInfo.getTitle());
                cosmeticViewHolder.rl_Feature.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Info info =new Info();
                        info.setTitle(featureInfo.getTitle());
                        info.setLike_count(featureInfo.getLike_count());
                        info.setUrl(featureInfo.getUrl());
                        Intent intent =new Intent(context,FeatureDetailsActivity.class);
                        intent.putExtra("featureInfo",info);
                        context.startActivity(intent);
                    }
                });

//                int size = 6;
//                int length = 100;
//                DisplayMetrics dm = new DisplayMetrics();
//                ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(dm);
//                float density = dm.density;
//                int gridviewWidth = (int) (size * (length + 4) * density);
//                int itemWidth = (int) (length * density);
//
//                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
//                        gridviewWidth, LinearLayout.LayoutParams.FILL_PARENT);
//                cosmeticViewHolder.gridview.setLayoutParams(params); // 设置GirdView布局参数,横向布局的关键
//                cosmeticViewHolder.gridview.setColumnWidth(itemWidth); // 设置列表项宽
//                cosmeticViewHolder.gridview.setHorizontalSpacing(5); // 设置列表项水平间距
//                cosmeticViewHolder.gridview.setStretchMode(GridView.NO_STRETCH);
//                cosmeticViewHolder.gridview.setNumColumns(size); // 设置列数量=列表集合数
              //  GridViewAdapter adapter = new GridViewAdapter(context, featureInfo);
                //cosmeticViewHolder.gridview.setAdapter(adapter);

                featureRecyclerViewAdapter=new FeatureRecyclerViewAdapter(context,featureInfo);
                ((CosmeticViewHolder) holder).recycleview_item_cosmetic.setAdapter(featureRecyclerViewAdapter);
                LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
                linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                ((CosmeticViewHolder) holder).recycleview_item_cosmetic.setLayoutManager(linearLayoutManager);
                break;

        }
    }



    @Override
    public int getItemCount() {
        return totalMap.size();
    }



    public class CosmeticViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout ll_Tutorial,ll_details,ll_background;
        private CircleImageView iv_head;
        private TextView tv_name, tv_title, tv_look, tv_comment, tv_like, tv_caption;
        private RecyclerImageView iv_background, iv_type, iv_icon;
        private Button btn_attention, btn_time;
        private RelativeLayout rl_icon, rl_Feature;
        private RecyclerView recycleview_item_cosmetic;


        public CosmeticViewHolder(View itemView) {
            super(itemView);
            ll_Tutorial = (LinearLayout) itemView.findViewById(R.id.ll_Tutorial);
            iv_head = (CircleImageView) itemView.findViewById(R.id.iv_head);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_look = (TextView) itemView.findViewById(R.id.tv_look);
            tv_comment = (TextView) itemView.findViewById(R.id.tv_comment);
            tv_like = (TextView) itemView.findViewById(R.id.tv_like);
            tv_caption = (TextView) itemView.findViewById(R.id.tv_caption);
            iv_background = (RecyclerImageView) itemView.findViewById(R.id.iv_background);
            iv_type = (RecyclerImageView) itemView.findViewById(R.id.iv_type);
            btn_attention = (Button) itemView.findViewById(R.id.btn_attention);
            btn_time = (Button) itemView.findViewById(R.id.btn_time);
            rl_Feature = (RelativeLayout) itemView.findViewById(R.id.rl_Feature);
            rl_icon = (RelativeLayout) itemView.findViewById(R.id.rl_icon);
            //gridview = (MyGridView) itemView.findViewById(R.id.gridview);
            recycleview_item_cosmetic= (RecyclerView) itemView.findViewById(R.id.recycleview_item_cosmetic);
            iv_icon = (RecyclerImageView) itemView.findViewById(R.id.iv_icon);
            ll_details= (LinearLayout) itemView.findViewById(R.id.ll_details);
            ll_background= (LinearLayout) itemView.findViewById(R.id.ll_background);
        }


    }
}
